#include "window_common.h"

#if (defined(GKT_CONFIG_DOOR_LOCK_AUTOMATIC) && GKT_CONFIG_DOOR_LOCK_AUTOMATIC)

enum {
	WIDGET_ID_LIST = 0,
	WIDGET_ID_MAX
};

enum {
	TIMER_ID_TIMEOUT = 0,
	TIMER_ID_MAX
};

static const gkt_widget_list_item_s sc_window_lock_auto_double_close_time_widget_list_items[] = {
	GKT_WIDGET_LIST_STATIC_ITEM(WINDOW_VOICE_COMBINED_ID(set_1),
		WINDOW_AUTO_DOUBLE_LOCK_TIME_SHORT),
	GKT_WIDGET_LIST_STATIC_ITEM(WINDOW_VOICE_COMBINED_ID(set_2),
		WINDOW_AUTO_DOUBLE_LOCK_TIME_MIDDLE),
	GKT_WIDGET_LIST_STATIC_ITEM(WINDOW_VOICE_COMBINED_ID(set_3),
		WINDOW_AUTO_DOUBLE_LOCK_TIME_LONG),
	GKT_WIDGET_LIST_STATIC_ITEM(WINDOW_VOICE_COMBINED_ID(set_4),
		WINDOW_AUTO_DOUBLE_LOCK_TIME_ZERO),
};

#define WINDOW_AUTO_DOUBLE_LOCK_TIME_LIST_ITEM_NUMS	\
	sizeof(sc_window_lock_auto_double_close_time_widget_list_items) / sizeof(gkt_widget_list_item_s)

static const gkt_widget_list_attr_s sc_window_auto_double_close_time_list_attr =
	GKT_WIDGET_LIST_STATIC_ATTR(WINDOW_AUTO_DOUBLE_LOCK_TIME_LIST_ITEM_NUMS,
		sc_window_lock_auto_double_close_time_widget_list_items);

GKT_WINDOW_WIDGET_DESCS_DEFINE(lock_auto_double_close_time) = {
	[WIDGET_ID_LIST] = GKT_WIDGET_LIST_DESC(0, &sc_window_auto_double_close_time_list_attr),
};

typedef struct _window_lock_time {
#if (GKT_MODULE_LOC_BACKBOARD == GKT_DOOR_LOCK_LOCATION)
	int	update_setting;
#endif
	uint32_t	auto_double_close_time;
	uint32_t play_over;

} window_lock_time_s;
static window_lock_time_s s_window_lock_time;

static void window_lock_auto_double_close_time_complete(window_result_e result)
{
	uint32_t voice_flags = GKT_WINDOW_VOICE_F_NOTIFY_END | GKT_WINDOW_VOICE_F_NOTIFY_ERROR;

	s_window_lock_time.update_setting = 0;

	if (WINDOW_RESULT_SET_SUCCESS == result) {
		if (s_window_lock_time.auto_double_close_time != gp_window_data->lock_auto_double_close_time) {
			gp_window_data->lock_auto_double_close_time = s_window_lock_time.auto_double_close_time;
			window_data_save();
		}
		gkt_window_voice_play(NULL, WINDOW_VOICE_COMBINED_ID(set_success), voice_flags, NULL);
	}
	else
		gkt_window_voice_play(NULL, WINDOW_VOICE_COMBINED_ID(set_failed), voice_flags, NULL);

	s_window_lock_time.play_over = 1;
}

#if (GKT_MODULE_LOC_BACKBOARD == GKT_DOOR_LOCK_LOCATION)
#define window_lock_auto_double_close_time_init()	\
	do { s_window_lock_time.update_setting = 0; } while (0)

static void window_lock_auto_double_close_time_update(void)
{
	int retval;

	retval = gkt_door_lock_set_auto_close_time(1, s_window_lock_time.auto_double_close_time);
	if (GKT_EINPROCESS == retval) {
		s_window_lock_time.update_setting = 1;
		gkt_window_timer_set(TIMER_ID_TIMEOUT, 2000, 0, 0);
	}
	else
		window_lock_auto_double_close_time_complete(WINDOW_RESULT_SET_FAILED);
}
#else
#define window_lock_auto_double_close_time_init()	do {} while (0)
#define window_lock_auto_double_close_time_update()	\
	window_lock_auto_double_close_time_complete(WINDOW_RESULT_SET_SUCCESS);
#endif

static int window_lock_auto_double_close_time_event_handler(
				uint32_t major_type, uint32_t minor_type,
				uint32_t id, uint32_t data)
{
	gkt_info("lock_auto_double_close_time: (%u)(%u-%d)\n",
		minor_type, id, (int32_t)data);

	switch (major_type) {
		case GKT_EVENT_WINDOW:
			if (minor_type == GKT_WINDOW_ACTIVE) {
				window_lock_auto_double_close_time_init();
				s_window_lock_time.play_over = 0;
				s_window_lock_time.auto_double_close_time = gp_window_data->lock_auto_double_close_time;
				window_common_mask_except_number_repeat_return(WINDOW_AUTO_DOUBLE_LOCK_TIME_LIST_ITEM_NUMS);
			}
			break;
		case GKT_EVENT_WIDGET:
			if (minor_type == GKT_WIDGET_EVENT_CLICK) {
				s_window_lock_time.auto_double_close_time =
					(gkt_window_id_t)sc_window_lock_auto_double_close_time_widget_list_items[data].data;
				window_lock_auto_double_close_time_update();
				return GKT_EVENT_HANDLE_COMPLETE;
			}
			break;
#if (GKT_MODULE_LOC_BACKBOARD == GKT_DOOR_LOCK_LOCATION)
		case GKT_EVENT_SYSTEM:
			if ((GKT_WINDOW_SYSTEM_DOOR == minor_type)
				&& (GKT_DOOR_NOTIFY_LOCK_CONFIG == id)
				&& s_window_lock_time.update_setting)
			{
				gkt_window_timer_clear(TIMER_ID_TIMEOUT);
				if (GKT_SUCCESS == data)
					window_lock_auto_double_close_time_complete(WINDOW_RESULT_SET_SUCCESS);
				else
					window_lock_auto_double_close_time_complete(WINDOW_RESULT_SET_FAILED);
			}
			break;
		case GKT_EVENT_TIMER:
			if ((minor_type == TIMER_ID_TIMEOUT)
				&& s_window_lock_time.update_setting)
			{
				window_lock_auto_double_close_time_complete(WINDOW_RESULT_SET_FAILED);
			}
			break;
#endif
		case GKT_EVENT_VOICE:
			if (s_window_lock_time.play_over == 1) {
				s_window_lock_time.play_over = 0;
				gkt_window_exit();
			}
			break;
		default:
			break;
	}

	return GKT_EVENT_HANDLE_CONTINUE;
}
GKT_WINDOW_DESC_DEFINE(lock_auto_double_close_time);

#endif

